#include "repoanalyzerworker.h"

#include "reposolutiontask.h"

#include <QDir>
#include <QThreadPool>
#include <QTimer>
#include <qd.h>
#include <repoanalyzermanager.h>
#include <repoutil.h>

#include "configutil.h"

RepoAnalyzerWorker::RepoAnalyzerWorker(RepoAnalyzerManager *manager)
    : QObject{}
    , manager{manager}
    , m_thread_pool(new QThreadPool)
{
    m_thread_pool->setMaxThreadCount(30);
}

void RepoAnalyzerWorker::solution(const QString &dir, const QString &name, const QString &path)
{
    auto fast_solution_task = new RepoSolutionTask;
    fast_solution_task->setDir(dir);
    fast_solution_task->setName(name);
    fast_solution_task->setPath(path);
    fast_solution_task->setManager(manager);
    fast_solution_task->setAutoDelete(true);
    fast_solution_task->setManual(true);
    m_thread_pool->start(fast_solution_task);
}

void RepoAnalyzerWorker::commit_fast_solution(const QString &dir, const QString &name, const QString &path)
{
    auto fast_solution_task = new RepoSolutionTask;
    fast_solution_task->setDir(dir);
    fast_solution_task->setName(name);
    fast_solution_task->setPath(path);
    fast_solution_task->setManager(manager);

    m_solution_tasks << fast_solution_task;
}

void RepoAnalyzerWorker::start_fast_solution()
{
    while(!m_solution_tasks.empty()) {
        m_thread_pool->start(m_solution_tasks.takeFirst());
    }
}

void RepoAnalyzerWorker::applySettings()
{
    auto config = ConfigUtil::getAppSettingsConfig();
    if (config.value("thread").toInt()>0)
        m_thread_pool->setMaxThreadCount(config.value("thread").toInt());
}
